package com.oner365.test.socket.connection;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ShortConnectionExecute implements Runnable {
  private static final Logger LOGGER = LoggerFactory.getLogger(ShortConnectionExecute.class);
  private Socket socket = null;
  private InputStream in = null;
  private OutputStream out = null;

  public ShortConnectionExecute(Socket socket) {
    this.socket = socket;
  }

  @Override
  public void run() {
    try {
      in = socket.getInputStream();
      Date day = new Date();
      SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      LOGGER.info(df.format(day));
      out = socket.getOutputStream();
      byte[] recData = new byte[1024];
      int length = in.read(recData);
      String body = new String(recData, 0, length);
      LOGGER.info("读取到客户端发送来的数据:{}" , body);
      out.write("已接收".getBytes());
    } catch (IOException e) {
      LOGGER.error("execute error:",e);
    } finally {
      try {
        in.close();
        out.close();
        socket.close();
      } catch (IOException e) {
        LOGGER.error("execute error:",e);
      }
    }
  }
}
